#include "globals.h"

void 
parse_server_in(
        int *server_port, 
        int *win
        )
{
    FILE * fp;

    int i;
    int j;
    int flags = 0;

    char buf[TMP_BUFSIZE] = "\0";

    if ((fp = fopen("server.in", "r")) == NULL)
    {
        printf("%s server.in not found\n", SRV_LOG);
        exit(1);
    }
    else
    {
        i = 1;

        while (Fgets(buf, TMP_BUFSIZE, fp) != NULL)
        {
            flags = 0;

            for (j = 0; j < strlen(buf) - 1 && isdigit(buf[j]); j++);

            if (j != strlen(buf) - 1)
            {
                flags=1;
            }

            switch (i)
            {
                case 1: 
                    if(flags || (*server_port = atoi(buf)) == 0)
                    {
                        printf("%s Error server.in: Line 1 contains invalid Port number\n", SRV_LOG);
                        exit(1);
                    }	

                    i++;
                    break;
                case 2:
                    if(flags || ( *win = atoi(buf)) == 0)
                    {
                        printf("%s Error server.in: Line 2 contains invalid Sending Sliding Window Size\n", SRV_LOG);
                        exit(1);
                    }	

                    i++;
                    break;
            }	

            bzero(buf, TMP_BUFSIZE);	
        }

        printf("%s Server on Port number %d and Sending Sliding Window Size %d\n",
                SRV_LOG, *server_port, *win);
        fclose(fp);
    }
}

int
set_interface_info(
        struct sock_ele s[],
        int server_port
        )
{
    int i = 1;
    int on = 1;

    struct ifi_info	*ifi;
    struct ifi_info *ifihead;	

    struct sockaddr_in *sa_in;
    struct sockaddr * sa;

    for (ifihead = ifi = Get_ifi_info_plus(AF_INET, 1), i=0; ifi != NULL; ifi = ifi->ifi_next, i++) 
    {
        s[i].sockfd = Socket(AF_INET, SOCK_DGRAM, 0);

        Setsockopt(s[i].sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));

        sa_in = (struct sockaddr_in *) ifi->ifi_addr;
        sa_in->sin_family = AF_INET;
        sa_in->sin_port = htons(server_port);
        Bind(s[i].sockfd, (SA *) sa_in, sizeof(*sa_in));
        strcpy(s[i].ip_addr,Sock_ntop((SA *) sa_in, sizeof(*sa_in)));
        sa =  ifi->ifi_ntmaddr;
        strcpy(s[i].net_mask,Sock_ntop_host(sa, sizeof(*sa)));

        get_subnet_mask(s[i].ip_addr, s[i].net_mask, s[i].sub_mask);

        printf("%s Interface %d\n\tIP Address :: %s\n\tNetwork Mask :: %s\n\tSubnet Mask :: %s\n\n",\ 
                SRV_LOG, (i + 1), s[i].ip_addr, s[i].net_mask, s[i].sub_mask);
    }

    free_ifi_info_plus(ifihead);

    return i;
}

int 
get_route_flags(
        char ip_server[],
        char ip_client[],
        char sub_mask[]
        )
{
    char check_sub_msk[INET_ADDRSTRLEN] = "\0";
    
    if (strcmp(ip_server, "127.0.0.1") == 0)
    {
        printf("%s Client and Server are on the same host\n\tIPclient::%s IPserver :: %s\n", 
                SRV_LOG, ip_client, ip_server);
        return MSG_DONTROUTE;
    }
    else 
    {
        //
        // Check whether client is local
        //

        get_subnet_mask(ip_client, sub_mask, check_sub_msk);

        if (strcmp(check_sub_msk, sub_mask) == 0)
        {
            printf("%s Client and Server are local i.e. in the same subnet\n\tIPclient::%s IPserver::%s\n", 
                    SRV_LOG, ip_client, ip_server);
            return MSG_DONTROUTE;        
        }
        else
        {
            printf("%s Client and Server are not local\n\tIPclient::%s IPserver::%s\n", 
                    SRV_LOG, ip_client, ip_server);
            return 0;        
        }								
    }
}

